//
//  NewFeatureController.swift
//  SinaBlog
//
//  Created by 李超 on 16/3/23.
//  Copyright © 2016年 lichao. All rights reserved.
//

import UIKit
let imageCount = 4
let layout = UICollectionViewFlowLayout()
private let reuseIdentifier = "Cell"

class NewFeatureController: UICollectionViewController {
    
    //通过重写构造方法来给collectionView赋值流水布局
     init() {
        super.init(collectionViewLayout: layout)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Register cell classes
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        //设置布局相关属性
        prepareLayout()

    }
    //设置collection流水布局相关属性
    func prepareLayout(){
        
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = view.bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.backgroundColor = UIColor.whiteColor()
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
  
    }
  
    //数据源方法，返回item数
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }
    //数据源方法，返回具体的cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        cell.imageIndex = indexPath.item
        
        return cell
    }

    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //获取当前页码
        let index = Int(scrollView.contentOffset.x / scrollView.bounds.size.width)
        let indexPath = NSIndexPath(forItem: index, inSection: 0)
        
        if indexPath.item == imageCount-1{
            //如果是最后一页则执行动画
           let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! NewFeatureCell
            cell.startAnimationForgoBntton()
            cell.btnAction = {
                //新特性后点击按钮跳转到主界面
                AppDelegate.outSwitchViewController(true)
            }
        
            
        }
        
        
    }
   
}
